package com.ekuaitu.kuaitustaff.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.ekuaitu.kuaitustaff.R;
import com.ekuaitu.kuaitustaff.activity.CarNewMainMapActivity;
import com.ekuaitu.kuaitustaff.activity.PhotoViewActivity;
import com.ekuaitu.kuaitustaff.activity.RecogCarTextActivity;
import com.ekuaitu.kuaitustaff.activity.ReturnCarCameraActivity;
import com.ekuaitu.kuaitustaff.utils.AppManager;
import com.ekuaitu.kuaitustaff.utils.Dialogs.LoadingDialog;
import com.ekuaitu.kuaitustaff.utils.Logger;
import com.ekuaitu.kuaitustaff.utils.StatusBarUtil;
import com.ekuaitu.kuaitustaff.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Jensen on 2016/8/12.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private Unbinder bind;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getLayoutId());
        bind = ButterKnife.bind(this);
        AppManager.getAppManager().addActivity(this);
        init();
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    @Override
    protected void onDestroy() {
        //提示
        ToastUtils.reset();
        super.onDestroy();
        bind.unbind();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private final void init() {
        //设置状态栏颜色 或者字体黑白
        if (this instanceof CarNewMainMapActivity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (StatusBarUtil.MIUISetStatusBarLightMode(getWindow(), true)) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        getWindow().setStatusBarColor(Color.TRANSPARENT);
                    } else {
                        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    }
                } else if (StatusBarUtil.FlymeSetStatusBarLightMode(getWindow(), true)) {
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    getWindow().setStatusBarColor(Color.TRANSPARENT);
                    Logger.i("zoule", "M");
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.0
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(Color.TRANSPARENT);
                    Logger.i("zoule", "L");
                }
            }
        } else if (this instanceof RecogCarTextActivity || this instanceof ReturnCarCameraActivity || this instanceof PhotoViewActivity) {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.black), 0, false);
        } else {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0, true);
        }
        loadingDialog = new LoadingDialog(this, true);
        initData();
        initView();
    }

    public void showLoadingDialog(String message) {
        try {
            if (loadingDialog != null && !loadingDialog.isShowing()) {
                loadingDialog.setTitle(message);
                loadingDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showLoadingDialog() {
        try {
            if (loadingDialog != null && !loadingDialog.isShowing()) {
                loadingDialog.setTitle(null);
                loadingDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismissLoadingDialog() {
        try {
            if (loadingDialog != null && loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

}
